<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends MY_Controller {
	
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'index';
        
        $this->load->model('Mindex');
    }
    
    
    public function index()
	{
		$this->checkLogin();
		$this->data['provinceid'] = $this->getParamInt('province');
		$this->data['districtid'] = $this->getParamInt('district');
		$this->data['s'] = $this->getParamString('s');
		$this->data['listprovince'] = $this->Mindex->listprovince();
	    $this->data['data'] = $this->Mindex->indexDB($this->data['provinceid'],$this->data['districtid'],$this->data['s']);
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
    
    public function place()
    {
        $this->checkLogin();
        $this->data['type'] = $this->getParamString('type');
        $this->data['id'] = $this->getParamInt('id');
        
        if($this->data['type'] == 'edit'){
            $this->data['dataedit'] = $this->Mindex->getPlaceInfo($this->data['id']);
            $this->data['district'] = $this->Mindex->getListDistrict($this->data['dataedit']->id_province);
        }else if($this->data['type'] == 'delete'){
            $this->Mindex->deletePlaceInfo($this->data['id']);
            redirect('place');
        }
        
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Mindex->getPlace($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        // h phan trang cho list.
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('district', 'Quận/huyện', 'required');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required');
        $this->form_validation->set_rules('name', 'Tên cửa hàng', 'required');
        if($this->form_validation->run() === TRUE)
        {
            if($this->checkZero($this->getParamInt('district')))
            {
                $this->data['zero'] = true;
            }else if($this->checkZero($this->getParamInt('district')))
            {
                $this->data['zero'] = true;
            }else{
                
                if($this->data['type'] == '')
                {
                    $run = $this->Mindex->savePlace();
                    if(!$run)
                    {
                        $this->data['error'] = 'Địa chỉ đã tồn tại';
                    }else{
                        $this->data['status'] = 'Thêm thành công.';
                    }   
                }else if($this->data['type'] == 'edit'){
                    $run = $this->Mindex->editPlace($this->data['id']);
                    if(!$run)
                    {
                        $this->data['error'] = 'Địa chỉ đã tồn tại';
                    }else{
                        $this->data['status'] = 'Cập nhật thành công.';
                    }  
                }
            }
        }
        
        $this->data['province'] = $this->getProvince();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
    }
    
    public function device()
    {
        $this->checkLogin();
        $this->data['type'] = $this->getParamString('type');
        $this->data['id'] = $this->getParamInt('id');
        
        if($this->data['type'] == 'edit'){
            $this->data['dataedit'] = $this->Mindex->getDeviceInfo($this->data['id']);
        }else if($this->data['type'] == 'delete'){
            $this->Mindex->deleteDeviceInfo($this->data['id']);
            redirect('device');
        }
        $this->data['geturlhomepage'] = $this->Mindex->geturlhomepage();
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Mindex->getDevice($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        // h phan trang cho list.
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namedevice', 'Tên thiết bị', 'required');
        $this->form_validation->set_rules('mac', 'Mac thiết bị', 'required');
		$this->form_validation->set_rules('nasid', 'NasID thiết bị', 'required');
        $this->form_validation->set_rules('version', 'Version hotspot', 'required');
        $this->form_validation->set_rules('types', 'Kiểu thiết bị của hệ thống hay riêng', 'required');
        $this->form_validation->set_rules('active', 'Trạng thái kích hoạt', 'required');
		
        if($this->form_validation->run() === TRUE)
        {
            if($this->checkZero($this->getParamInt('place')))
            {
                $this->data['zero'] = true;
            }else{
                
                if($this->data['type'] == '')
                {
                    $fileUpload = null;
                    if($_FILES['fileconfig'] != null)
                    {
                        $upload = $this->uploadFile();
                        if($upload['status'] == 201)
                        {
                            $this->data['error'] = 'Upload file thất bại.';
                        }else{
                            $fileUpload = $upload['data'];
                        }
                    }
                    
                    $run = $this->Mindex->saveDevice($fileUpload);
                    if(!$run)
                    {
                        $this->data['error'] = 'Địa chỉ đã tồn tại.';
                    }else{
                        $this->data['status'] = 'Thêm thành công.';
                    }   
                }else if($this->data['type'] == 'edit'){
                    $fileUpload = null;
                    if($_FILES['fileconfig'] != null)
                    {
                        $upload = $this->uploadFile();
                        if($upload['status'] == 201)
                        {
                            $this->data['error'] = 'Upload file thất bại.';
                        }else{
                            $fileUpload = $upload['data'];
                        }
                    }
                    $run = $this->Mindex->editDevice($this->data['id'], $fileUpload);
                    if(!$run)
                    {
                        $this->data['error'] = 'Địa chỉ đã tồn tại.';
                    }else{
                        $this->data['status'] = 'Cập nhật thành công.';
                    }  
                }
            }
        }
        
        $this->data['place'] = $this->Mindex->getPlaceSelect();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
    }
    
    private function checkZero($str)
    {
        if($str < 1)
        {
            return true;
        }else{
            return false;
        }
    }
    
    public function getDistrict()
    {
        die($this->data['fview']->selectOption($this->getDistrictData($this->getParamInt('id')), '', 'form-control', 'district', 'district'));
    }
    
    public function updatestatus()
    {
        die();
    }
    
    public function warning()
    {
        die($this->Mindex->warningSystemDB());
    }
    
    private function uploadFile()
    {
        $config = array(
            'upload_path' => PROJECT_PATH."/public/",
            'allowed_types' => "zip",
            'encrypt_name'  => true,
            'max_size' => "20480000"
        );

		$this->load->library('upload', $config);
        
        
		if ( ! $this->upload->do_upload('fileconfig'))
		{		  
			return array('status' => 201, 'data' => null);
		}
		else
		{
		    $dataFile = $this->upload->data();
			return array('status' => 200, 'data' => $dataFile['file_name']);
		}
    }
    
    public function checkupdate()
    {
        $this->Mindex->updatestatusDB();
        die($this->Mindex->checkupdateDB($this->getParamString('id')));
    }
    
    public function getReport()
    {
        $this->data['data'] = $this->Mindex->getReportDb();
        
        $this->data['action'] = 'report';
		$this->load->view('temp/index/report', $this->data);
    }
	
	public function indexgetdistrict(){
		die($this->Mindex->indexgetdistrict($this->getParamInt('province')));
	}
	
	public function indexlistdistrict(){
		die($this->Mindex->indexlistdistrict($this->getParamInt('province'),$this->getParamInt('district')));
	}
	
	public function devicedetail(){
		$id = $this->getParamUri(2,1);
		if( in_array($id,array('',null)) ){
			redirect('');
		}
		$this->data['id'] = $id;
		$this->load->library('form_validation');
        $this->form_validation->set_rules('content', 'Nội dung ghi chú', 'required');
		if($this->form_validation->run() === TRUE)
        {
			$content = $this->getParamString('content');
			$run = $this->Mindex->addCommentDb($id,$content);
			if( $run == 200 ){
				redirect('device-detail/'.$id);
			}else{
				redirect('device-detail/'.$id);
			}
		}
		
		$this->data['devicedetail'] = $this->Mindex->devicedetail($id);
		$this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
	
	public function ajaxupdatecomment(){
		die(json_encode($this->Mindex->ajaxupdatecomment()));
	}
	
}
